<?php

namespace App\Admin\Controllers;

use App\Models\Classroom;
use App\Models\Crouse_time;
use App\Models\Squad;
use App\Models\Crouse;
use App\Models\Student;
use App\Models\Notice;
use App\Models\Crousesheet;
use App\Models\Zuoye_config;
use App\Models\Term;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;

class Zuoye_configController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.zuoye_config'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.zuoye_config'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.zuoye_config'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Zuoye_config::class, function (Grid $grid) {
            //超级管理员可看到所有数据
            if (Admin::user()->isAdministrator()) {
                
            }else{
                $grid->model()->where('create_user',Admin::user()->id);
            }
            
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            
            $grid->zuoye_desc(trans('admin.zuoye_desc'));
            $grid->zuoye_week('时间');
            $grid->squad_id('班级')->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            $grid->crouse_id('课程')->display(function($crouse_id){
                return Crouse::find($this->crouse_id)->name;
            });
            $grid->create_user('配置人')->display(function($create_user){
                return DB::table('admin_users')->find($this->create_user)->en_name;
            });
            //禁用导出按钮
            $grid->disableExport();
            $grid->disableFilter();
            $grid->disableActions();
            $grid->disableRowSelector();
            // $grid->tools(function ($tools) { 
            //     //关闭批量删除 
            //     $tools->batch(function ($batch) { 
            //         $batch->disableDelete(); 
            //     });
            // });
        });
    }

    protected function form()
    {
        return Admin::form(Zuoye_config::class, function (Form $form) {
            $term_start_date = Term::where('status','0')->value('start_date');
            $week =  ceil((time()-strtotime($term_start_date))/3600/24/7);
            $form->display('id', 'ID');
            $form->text('zuoye_desc','作业描述')->addElementLabelClass('must-input')->rules('required');
            for ($i=1; $i < 25; $i++) {
                $j = $i-1;
                $date_arr[$i] = '第'.$i.'周【'.date('Y/m/d',strtotime("$term_start_date +$j weeks")).'-'.date('Y/m/d',strtotime("$term_start_date +$i weeks -1 days")).'】';
            }
            $form->select('zuoye_week','周')->options($date_arr)->default($week)->rules('required')->addElementLabelClass('must-input');
            //找出当前登陆用户所带的班级
            $squads = Squad::where('class_teacher',Admin::user()->id)->pluck('name','id')->toArray();
            
            $form->select('squad_id',' 班级')->options($squads)->load('crouse_id','/api/get_squad_crouses')->addElementLabelClass('must-input')->rules('required');
            $form->select('crouse_id','课程')->addElementLabelClass('must-input')->rules('required');
            $form->hidden('create_user')->value(Admin::user()->id);
            $form->saving(function(Form $form){
                $form->zuoye_week = Term::where('status','0')->value('name')."第".$form->zuoye_week."周";
            });
            $form->saved(function (Form $form) use($term_start_date){
                //作业配置添加完成后，同时生成一条待办记录
                $zuoye_config_id= $form->model()->id;
                $zuoye_week = Zuoye_config::where('id',$zuoye_config_id)->value('zuoye_week');
                //dd($zuoye_week);
                $squadId = $form->squad_id;
                $squadName = Squad::where('id',$squadId)->value('name');
                $crouseId = $form->crouse_id;
                $crouseName = Crouse::where('id',$crouseId)->value('name');
                $tutor_id = Squad::where('id',$squadId)->value('class_teacher');
                $selected_week = '';
                for ($i=0; $i < strlen($zuoye_week); $i++) { 
                    if (is_numeric($zuoye_week[$i])) {
                        $selected_week .= $zuoye_week[$i];
                    }
                }
                
                $selected_weeks = str_split($selected_week, 4);
                $selected_week = $selected_weeks[1];
                //dd($selected_week);
                $grace_days = 14;
               
                $starttime = date('Y-m-d 00:00:00',strtotime("+$term_start_date +$selected_week weeks -1 weeks"));
                //dd($starttime);
                $endtime = date("Y-m-d H:i:s",strtotime("+".$grace_days."days",strtotime($starttime)));
                if ($endtime<date('Y-m-d H:i:s')) {
                    $status = '3';
                }else{
                    $status = '1';
                }
                // dd($starttime);
                $data[] = [
                    'notice_type' =>'5',
                    'zuoye_config_id' =>$zuoye_config_id,
                    'event' =>'作业-'.$squadName.'-'.$form->zuoye_desc.'-'.$zuoye_week.'-'.$crouseName,
                    'starttime' =>$starttime,
                    'grace_days'=>$grace_days,
                    'endtime' =>$endtime,
                    'status'=>$status,
                    'squad_id' => $squadId,
                    'tutor_id' => $tutor_id
                ];
                //dd($data);
                $res = Notice::insert($data);
            });
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }
}
